package com.zx.sa.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/tool")
public class ChatToolController {

    private final ChatClient chatClient;

    @Autowired
    public ChatToolController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @RequestMapping("/index")
    public String index() {
        return "tool";
    }


    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ResponseBody
    public Flux<String> chat(String msg) {
        return chatClient.prompt()
                .user(msg)
                .stream()
                .content();
    }

    @RequestMapping(value = "/time")
    @ResponseBody
    public String time() {
        return chatClient
                .prompt("What day is tomorrow?")
                .tools(new DateTimeTools())
                .call()
                .content();
    }

    public static class DateTimeTools {
        @Tool(description = "Get the current date and time in the user's timezone")
        public String getCurrentDateTime() {
            String s = LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
            System.out.printf("666:" + s);
            return s;
        }
    }
}